 <?php
class Stock extends CActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return "ts_stock";
    }
    
    public function attributeLabels() {
        return array(
            "product_id" => "สินค้า",
            "stock_product_quantity" => "จำนวนสินค้า",
            "stock_description" => "รายละเอียดสินค้า",
            "stock_status" => "สถานะ",
            "stock_create_date" => "วันที่สร้าง",
            "stock_modified_date" => "วันที่แก้ไข",
        );
    }
    
    public function rules() {
        return array(
            array("stock_product_quantity", "required",'message'=>'กรุณากรอกข้อมูลให้ครบ'),
            array("stock_product_quantity", "length", "max" => 20),
            array('stock_product_quantity', 'numerical', 'integerOnly'=>true)
        );
    }
    
    public function search() {
        $criteria = new CDbCriteria;
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function relations() {
        return array(
            "product" => array(self::BELONGS_TO, "Product", "product_id")
        );
    }
    
    public function beforeValidate() {
        if ($this->isNewRecord) {
            $this->stock_status = 1;
            $this->stock_create_date = new CDbExpression("NOW()");
        }
        else
        {
            $this->stock_modified_date = new CDbExpression("NOW()");
        }

        return parent::beforeValidate();
    }
}
?>
